import mongoose, { Document, Schema } from 'mongoose'

export interface IDanmaku extends Document {
  roomId: mongoose.Types.ObjectId
  userId: mongoose.Types.ObjectId
  content: string
  color: string
  type: 'scroll' | 'top' | 'bottom'
  time: number // 视频时间点
  createdAt: Date
}

const DanmakuSchema = new Schema({
  roomId: {
    type: Schema.Types.ObjectId,
    ref: 'Room',
    required: true,
  },
  userId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
  content: {
    type: String,
    required: true,
    maxlength: 100,
  },
  color: {
    type: String,
    default: '#ffffff',
  },
  type: {
    type: String,
    enum: ['scroll', 'top', 'bottom'],
    default: 'scroll',
  },
  time: {
    type: Number,
    required: true,
    min: 0,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
})

// 创建索引
DanmakuSchema.index({ roomId: 1, time: 1 })
DanmakuSchema.index({ userId: 1, createdAt: -1 })

export const Danmaku = mongoose.model<IDanmaku>('Danmaku', DanmakuSchema)
